/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "ProjectSerializer.h"

#include "NativeFile.h"
#include "GameObjectSerializer.h"
#include "ProjectResourceManager.h"
#include "ProjectManager.h"

namespace PQEngine
{
	ProjectSerializer::ProjectSerializer()
	{

	}

	ProjectSerializer& ProjectSerializer::getSingleton()
	{
		static ProjectSerializer inst;
		return inst;
	}

	void ProjectSerializer::saveResourceLocation()
	{
		std::vector<std::string> pathes=ProjectResourceGroup::getSingleton().getResourceGroupPathes();

		int size=pathes.size();
		GDataGenerator gen(GDATA_RESOURCE_LOCATION);
		gen.addIntParam(size);
		for(int i=0;i<size;i++){
			gen.addStringParam(pathes[i]);
		}
		gen.serialize("ResourceLocation.save");

	}

	void ProjectSerializer::loadResourceLocation()
	{
		GDataParser parser("ResourceLocation.save");
		GDataName name= parser.getName();
		if(name!=GDATA_RESOURCE_LOCATION){
			return;//illegal file
		}

		GDataParam* param=parser.getParam();
		if(!GDataParam::isIntParam(param)){
			return;
		}

		std::vector<std::string> pathes;
		int size=param->getInt();
		for(int i=0;i<size;i++){
			param=parser.getParam();
			if(GDataParam::isStringParam(param)){
				pathes.push_back(param->getString());
			}
		}

		for(int i=0;i<size;i++){
			ProjectResourceGroup::getSingleton().addResource(pathes[i]);
		}
	}

	void ProjectSerializer::saveGameObjects()
	{
		GDatagram* dgram=GameObjectSerializer::getSingleton().serialize();
		NativeFile::write("GameObjects.save",dgram);
	}

	void ProjectSerializer::loadGameObjects()
	{
		GDatagram* dgram=NativeFile::readDatagram("GameObjects.save");

		GameObjectSerializer::getSingleton().unserializer(dgram);
	}

	void ProjectSerializer::saveProject()
	{
		saveCameraSetting();
		saveResourceLocation();
		saveGameObjects();
		
	}

	void ProjectSerializer::loadProject()
	{
		loadCameraSetting();
		loadResourceLocation();
		loadGameObjects();
	}

	void ProjectSerializer::saveCameraSetting()
	{
		/*
		Ogre::Vector3 position=GameBuilderCamera::getSingleton().getPivotPosition();
		Ogre::Quaternion orientation=GameBuilderCamera::getSingleton().getPivotOrientation();

		GDataGenerator gen(GDATA_SCENE_CAMERA);
		gen.addFloatParam(position.x);
		gen.addFloatParam(position.y);
		gen.addFloatParam(position.z);

		gen.addFloatParam(orientation.w);
		gen.addFloatParam(orientation.x);
		gen.addFloatParam(orientation.y);
		gen.addFloatParam(orientation.z);

		gen.serialize("Camera.save");
		*/
	}

	void ProjectSerializer::loadCameraSetting()
	{
		Ogre::Vector3 position;
		Ogre::Quaternion orientation;

		GDatagram* dgram=NativeFile::readDatagram("Camera.save");

		GDataParser parser(dgram);
		GDataName name= parser.getName();
		if(name!=GDATA_SCENE_CAMERA){
			return;//illegal file
		}

		GDataParam* param=parser.getParam();
		if(GDataParam::isFloatParam(param)){
			position.x=param->getFloat();
		}

		param=parser.getParam();
		if(GDataParam::isFloatParam(param)){
			position.y=param->getFloat();
		}

		param=parser.getParam();
		if(GDataParam::isFloatParam(param)){
			position.z=param->getFloat();
		}
		

		param=parser.getParam();
		if(GDataParam::isFloatParam(param)){
			orientation.w=param->getFloat();
		}

		param=parser.getParam();
		if(GDataParam::isFloatParam(param)){
			orientation.x=param->getFloat();
		}

		param=parser.getParam();
		if(GDataParam::isFloatParam(param)){
			orientation.y=param->getFloat();
		}

		param=parser.getParam();
		if(GDataParam::isFloatParam(param)){
			orientation.z=param->getFloat();
		}

		delete dgram;
		
		/*
		GameBuilderCamera::getSingleton().setPivotPosition(position);
		GameBuilderCamera::getSingleton().setPivotOrientation(orientation);
		*/
	}

}
